package com.leilei.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leilei.reggie.common.R;
import com.leilei.reggie.entity.Employee;
import com.leilei.reggie.entity.OrderDetail;
import com.leilei.reggie.entity.Orders;

import com.leilei.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/order")
public class OrderController {


    @Autowired
    private OrderService orderService;



    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){

        log.info("订单数据：{}",orders);

        orderService.submit(orders);

        return R.success("下单成功");

    }


    /**
     * 分页显示订单信息
     * @param page
     * @param pageSize
     * @param number
     * @return
     */
    @GetMapping(value = {"/page","/userPage"})
    public R<Page> page(int page,int pageSize,String number){

        log.info("page={},pageSize={},number={}",page,pageSize,number);

        //构造分页构造器
        Page pageInfo = new Page(page,pageSize);

        //构造条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();

        //添加过滤条件
        queryWrapper.eq(StringUtils.isNotEmpty(number),Orders::getNumber,number);

        //添加排序条件
        queryWrapper.orderByDesc(Orders::getOrderTime);

        //执行查询
        orderService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);

    }


    /**
     * 更新订单状态
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Orders orders){

        log.info(orders.toString());

        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(Orders::getId,orders.getId());

        Orders orderServiceOne = orderService.getOne(queryWrapper);

        orderServiceOne.setStatus(orderServiceOne.getStatus()+1);

        LambdaUpdateWrapper<Orders> updateWrapper = new LambdaUpdateWrapper<>();

        updateWrapper.eq(Orders::getId,orders.getId());

        orderService.update(orderServiceOne,updateWrapper);

        return R.success("状态更新成功");

    }



}
